/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cstdint>
#include <memory>
#include "json_error.h"
#include "yyjson.h"
#include "json_implement.h"
#include "yyjson_util.h"
#include "securec.h"
#include "json_utils.h"

constexpr size_t PARAMETER_ONE = 1;
constexpr size_t PARAMETER_TWO = 2;
constexpr size_t NO_FLAG = 0;

using namespace json_error;
using namespace yyjson_util;

napi_value ParseJson(napi_env env, napi_value arg0, uint32_t flag)
{
    size_t strLen;
    HANDLE_NAPI_ERROR(env, napi_get_value_string_utf8(env, arg0, nullptr, 0, &strLen));
    
    std::unique_ptr<char[]> jsonStr = std::make_unique<char[]>(strLen + YYJSON_PADDING_SIZE);
    HANDLE_NAPI_ERROR(env, napi_get_value_string_utf8(env, arg0, jsonStr.get(), strLen + YYJSON_PADDING_SIZE, &strLen));
    
    return json_implement::ParseJson(env, jsonStr.get(), strLen, flag);
}

napi_value json_utils::Parse(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_ONE;
    napi_value args[PARAMETER_ONE] = {nullptr};
    
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    
    if (argc < PARAMETER_ONE) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    return ParseJson(env, args[0], NO_FLAG);
}

napi_value json_utils::ParseWithOptions(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_TWO;
    napi_value args[PARAMETER_TWO] = {nullptr};
    
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    
    if (argc < PARAMETER_TWO) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    uint32_t flag;
    HANDLE_NAPI_ERROR(env, napi_get_value_uint32(env, args[1], &flag));
    
    return ParseJson(env, args[0], flag);
}

napi_value json_utils::Stringify(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_ONE;
    napi_value args[PARAMETER_ONE];
    
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    
    if (argc < PARAMETER_ONE) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    return json_implement::Stringify(env, args[0], NO_FLAG);
}

napi_value json_utils::StringifyToBuffer(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_ONE;
    napi_value args[PARAMETER_ONE];
    
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    
    if (argc < PARAMETER_ONE) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    return json_implement::StringifyToBuffer(env, args[0], NO_FLAG);
}

napi_value json_utils::StringifyWithOptions(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_TWO;
    napi_value args[PARAMETER_TWO];
    
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    
    if (argc < PARAMETER_TWO) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    uint32_t flag;
    HANDLE_NAPI_ERROR(env, napi_get_value_uint32(env, args[1], &flag));
    
    return json_implement::Stringify(env, args[0], flag);
}

napi_value json_utils::StringifyToBufferWithOptions(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_TWO;
    napi_value args[PARAMETER_TWO];
    
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    
    if (argc < PARAMETER_TWO) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    uint32_t flag;
    HANDLE_NAPI_ERROR(env, napi_get_value_uint32(env, args[1], &flag));
    
    return json_implement::StringifyToBuffer(env, args[0], flag);
}

napi_value ParseInc(napi_env env, napi_value arg0, uint32_t flag)
{
    bool isArrayBuffer = false;
    HANDLE_NAPI_ERROR(env, napi_is_arraybuffer(env, arg0, &isArrayBuffer));
    if (!isArrayBuffer) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_TYPE_ERROR);
    }
    
    void *data = nullptr;
    size_t byteLength = 0;
    HANDLE_NAPI_ERROR(env, napi_get_arraybuffer_info(env, arg0, &data, &byteLength));
    
    return json_implement::ParseInc(env, (char*)data, byteLength, flag);
}

napi_value json_utils::ParseInc(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_ONE;
    napi_value args[PARAMETER_ONE];
    
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    
    if (argc < PARAMETER_ONE) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    return ParseInc(env, args[0], NO_FLAG);
}

napi_value json_utils::ParseIncWithOptions(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_TWO;
    napi_value args[PARAMETER_TWO];
    
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    
    if (argc < PARAMETER_TWO) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    uint32_t flag;
    HANDLE_NAPI_ERROR(env, napi_get_value_uint32(env, args[1], &flag));
    
    return ParseInc(env, args[0], flag);
}

napi_value ParseIncAsync(napi_env env, napi_value arg0, uint32_t flag)
{
    bool isArrayBuffer = false;
    HANDLE_NAPI_ERROR(env, napi_is_arraybuffer(env, arg0, &isArrayBuffer));
    if (!isArrayBuffer) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_TYPE_ERROR);
    }
    
    void *data = nullptr;
    size_t byteLength = 0;
    HANDLE_NAPI_ERROR(env, napi_get_arraybuffer_info(env, arg0, &data, &byteLength));
    
    napi_deferred deferred;
    napi_value promise;
    HANDLE_NAPI_ERROR(env, napi_create_promise(env, &deferred, &promise));
    
    napi_value resource_name;
    HANDLE_NAPI_ERROR(env, napi_create_string_utf8(env, "ParseIncAsync", NAPI_AUTO_LENGTH, &resource_name));
    AsyncData* asyncData = new AsyncData{nullptr, deferred, static_cast<char*>(data), byteLength, flag, nullptr,
                                                                                                        false, ""};
    napi_async_work work;
    HANDLE_NAPI_ERROR(env, napi_create_async_work(env, nullptr, resource_name, ParseIncAsync, ParseIncAsyncCompleted,
                                                  asyncData, &work));
    HANDLE_NAPI_ERROR(env, napi_queue_async_work(env, work));
    return promise;
}

napi_value json_utils::ParseIncAsync(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_ONE;
    napi_value args[PARAMETER_ONE];
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    if (argc < PARAMETER_ONE) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    return ParseIncAsync(env, args[0], NO_FLAG);
}

napi_value json_utils::ParseIncAsyncWithOptions(napi_env env, napi_callback_info info)
{
    size_t argc = PARAMETER_TWO;
    napi_value args[PARAMETER_TWO];
    HANDLE_NAPI_ERROR(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
    if (argc < PARAMETER_TWO) {
        HANDLE_NAPI_ERROR(env, NAPI_PARAM_NUMBER_ERROR);
    }
    
    uint32_t flag;
    HANDLE_NAPI_ERROR(env, napi_get_value_uint32(env, args[1], &flag));
    
    return ParseIncAsync(env, args[0], flag);
}